<?php 
/*
 * Livra Code (http://code.google.com/p/livra-api/)
 * author: livracode@gmail.com
 * 
 * This class has the behaviour to call the methods of the api.
 *
 * TODO: HANDLE ERRORS
 */
 
require_once('LivraConstants.php');
require_once('OAuth.php');
class LivraClient {

  private $livraOAuth = null;
  
  /**
   * this method requires the LivraOAuth component already authorized.
   * @parameter LivraOAuth
   * see index.php (as an example)
   *
   */
  function __construct($livraOAuth  = NULL ) {
   
    $this->livraOAuth = $livraOAuth;
    if($this->livraOAuth == null && LivraOAuth::hasObjectInSession()){
        $this->livraOAuth = LivraOAuth::getBySession();
    }
  }
  /**
   * This method return the pick as an xml object
   * @parameter ex: array('id'=>'xxxxxx') xxxxxx = integer
   */
  public function getPick($params){
    $content = $this->oAuthRequest(LivraConstants::getPickUrl(), $params, 'GET');
    $xml = @simplexml_load_string($content);
    return $xml;
  }
  
  /**
   * This method retrieves all the Picks for the parameter filters.
   * @parameter ex: array( 'criterion' => 'MOST_RELEVANT', 'category_id' => 'xxxxxx') xxxxxx = integer
   */  
  public function getPicks($params){
    $content = $this->oAuthRequest(LivraConstants::getPicksUrl(), $params, 'GET');
    //echo $content;
    $xml = @simplexml_load_string($content);
    return $xml;
  }
  /**
   * This method return all the categories as xml.
   */
  public function getCategories(){
    $content = $this->oAuthRequest(LivraConstants::getCategoriesUrl(), array(), 'GET');
    $xml = @simplexml_load_string($content);
    return $xml;
  }
  /**
   * This method votes a pick.
   */
  public function votePick($params){
    $content = $this->livraOAuth->oAuthRequestSign(LivraConstants::getVotePickUrl(), $params, 'GET');
  }



 /**
   * Format and sign an OAuth / API request
   */
  function oAuthRequest($url, $args = array(), $method = NULL) {/*{{{*/
    if (empty($method)) $method = empty($args) ? "GET" : "POST";
    
    switch ($method) {
    
    case 'GET': return $this->http($this->to_url($url,$args));
    case 'POST': return $this->http($this->get_normalized_http_url($url), $this->to_postdata($args));
    }
    
  }/*}}}*/



  /**
   * builds a url usable for a GET request
   */
  public function to_url($url,$param) {
    $post_data = $this->to_postdata($param);
    $out = $this->get_normalized_http_url($url);
    if ($post_data) {
      $out .= '?'.$post_data;
    }
    return $out;
  }




/**
   * builds the data one would send in a POST request
   */
  public function to_postdata($parameters) {
    return OAuthUtil::build_http_query($parameters);
  }


  /**
   * parses the url and rebuilds it to be
   * scheme://host/path
   */
  public function get_normalized_http_url($http_url) {
    $parts = parse_url($http_url);

    $port = @$parts['port'];
    $scheme = $parts['scheme'];
    $host = $parts['host'];
    $path = @$parts['path'];

    $port or $port = ($scheme == 'https') ? '443' : '80';

    if (($scheme == 'https' && $port != '443')
        || ($scheme == 'http' && $port != '80')) {
      $host = "$host:$port";
    }
    return "$scheme://$host$path";
  }


  /**
   * Make an HTTP request
   *
   * @return API results
   */
  function http($url, $post_data = null) {/*{{{*/
    $ch = curl_init();
    if (defined("CURL_CA_BUNDLE_PATH")) curl_setopt($ch, CURLOPT_CAINFO, CURL_CA_BUNDLE_PATH);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //////////////////////////////////////////////////
    ///// Set to 1 to verify Twitter's SSL Cert //////
    //////////////////////////////////////////////////
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    if (isset($post_data)) {
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    $response = curl_exec($ch);
    $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $this->last_api_call = $url;
    curl_close ($ch);
    return $response;
  }/*}}}*/



  public function getCriterions(){
    return array( "MOST_RELEVANT"=>"most relevant",
                  "MOST_USEFUL"=>"most useful",
                  "MOST_VOTED"=>"most voted",
                  "MOST_VOTER"=>"most voter",
                  "MOST_RATED"=>"most rated",
                  "MOST_FAVORITED"=>"most favorited",
                  "MOST_COMMENTED"=>"most comented",
                  "MOST_RECENT"=>"most recent",
                  "MOST_ACTIVE"=>"most active",
                  "FRIENDS"=>"friends",
                  "MINE"=>"mine");
  }
  
}

?>